<template>
  <van-cell-group title="反馈审核" class="panel-feed-approve">
    <van-field required class="input-area" label-width="70px" label="反馈意见" input-align="right" disabled :value="detail ? detail.feedback.feedback_reason : ''"/>
    <van-field required class="input-area" label-width="70px" label="反馈扣分" input-align="right" disabled :value="detail ? detail.feedback.feedback_score : ''"/>
    <van-row class='radio-area' gutter="10">
      <van-radio-group v-model="checkResult" direction="horizontal">
        <van-col span="10" offset="2">
          <van-radio name="0">审核拒绝</van-radio>
        </van-col>
        <van-col span="10" offset="2">
          <van-radio name="1">审核通过</van-radio>
        </van-col>
      </van-radio-group>
    </van-row>
    <van-row class="split-h last-row"/>
    <van-field class='input-area' label-width="70px" v-if="checkResult === '0'" required v-model="refuseReason" rows="1" autosize label="拒绝原因" type="textarea" maxlength="100" placeholder="请输入审核拒绝原因" show-word-limit/>
    <template v-if="checkResult === '1'">
      <card-receivers v-if="detail" :parentPath="assessType + '' === $vo.AssessType.CORP + '' ? detail.problem_inst.dwmc : detail.problem_inst.khbm" v-model="receivers"/>
      <van-row class="split-h last-row"/>
    </template>
    <van-row class='operate-area'>
      <van-button type="info" :disabled="checkResult === ''" @click="submit">提交审核结果</van-button>
    </van-row>
    <loading-overlay :show="isLoading"/>
  </van-cell-group>
</template>

<script>
import CardReceivers from '@/views/common/card/CardReceivers'
import { updateProblemInst } from '@/api/assess'
export default {
  name: 'PanelFeedApprove',
  components: { CardReceivers },
  props: ['detail', 'assessType'],
  data () {
    return {
      isLoading: false,
      checkResult: '',
      refuseReason: '',
      receivers: []
    }
  },
  mounted () {
    console.log('detail', this.detail)
  },
  methods: {
    submit () {
      if (this.checkResult === '') {
        this.$dialog.alert({
          message: '请选择审核结果'
        })
        return
      }
      if (this.checkResult === '0' && this.refuseReason.trim() === '') {
        this.$dialog.alert({
          message: '请输入审核拒绝原因'
        })
        return
      }
      if (this.checkResult === '1' && this.receivers.length === 0) {
        this.$dialog.alert({
          message: '请选择接收人员'
        })
        return
      }
      this.$dialog.confirm({
        message: '确认提交审核结果?'
      }).then(() => {
        this.doUpdateProblem(this.getSubmitObj())
      }).catch(() => {
      })
    },
    getSubmitObj () {
      let opcontent = this.$vo.createOperate()
      opcontent.approve_userid = this.$store.state.authInfo.user.userid
      opcontent.approve_username = this.$store.state.authInfo.user.username
      opcontent.approve_result = this.checkResult
      opcontent.problem_id = this.detail.problem_inst.id
      opcontent.refuse_reason = this.checkResult === '0' ? this.refuseReason : ''
      opcontent.receivers = this.receivers
      let submitObj = {
        op: this.$vo.OperateType.APPROVE,
        opcontent: opcontent,
        problem_inst: this.detail.problem_inst
      }
      return submitObj
    },
    doUpdateProblem (obj) {
      // console.log('submit', obj)
      this.isLoading = true
      updateProblemInst(obj).then(res => {
        this.isLoading = false
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          this.$toast.success('评分结果提交成功')
          this.$emit('onConfirm')
        } else {
          this.$dialog.alert({ message: '评分结果提交失败：' + data[this.$response.MESSAGE] })
        }
      }, errFunc => {
        this.isLoading = false
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    }
  }
}
</script>

<style scoped lang="less">
  .panel-feed-approve {
    .radio-area {
      padding: 14px 16px;
      font-size: 12px;
    }
    .input-area {
      padding: 10px 16px;
      font-size: 12px;
    }
    .operate-area {
      padding: 20px 16px;
      .van-button {
        width: 100%;
      }
    }
    .last-row {
      margin-left: 16px;
    }
  }
</style>
